package cn.hg.jboot.core.controller;

import java.util.HashMap;
import java.util.List;

import com.jfinal.core.NotAction;
import com.jfinal.kit.HttpKit;
import com.jfinal.kit.Kv;
import com.jfinal.upload.UploadFile;

import cn.hg.jboot.core.constant.BaseConstants;
import io.jboot.web.controller.JbootController;

/**
 * 基础Controller，里面的很多方法是从JBoot中照搬过来的
 *
 * @author Golly
 */
public abstract class BaseController extends JbootController {

    private static final String BODY_STRING_ATTR = "__body_str";
    private static final Object NULL_OBJ = new Object();

    /**
     * 清除Session，通常用于登出操作
     */
    protected void clearSession() {
        while (this.getSession().getAttributeNames().hasMoreElements()) {
            String name = this.getSession().getAttributeNames().nextElement();
            this.getSession().removeAttribute(name);
        }
    }


    @NotAction
    public String getBodyString() {
        Object object = this.getAttr(BaseController.BODY_STRING_ATTR);

        if (object == BaseController.NULL_OBJ) {
            return null;
        }

        if (object != null) {
            return (String)object;
        }

        object = HttpKit.readData(this.getRequest());

        if (object == null) {
            this.setAttr(BaseController.BODY_STRING_ATTR, BaseController.NULL_OBJ);
        } else {
            this.setAttr(BaseController.BODY_STRING_ATTR, object);
        }

        return (String)object;
    }

    /**
     * 获取所有上传的文件
     *
     * @return
     */
    @NotAction
    public HashMap<String, UploadFile> getUploadFilesMap() {

        if (!this.isMultipartRequest()) {
            return null;
        }

        List<UploadFile> fileList = this.getFiles();
        HashMap<String, UploadFile> filesMap = null;

        if (fileList != null && fileList.size() > 0) {
            filesMap = new HashMap<>();

            for (UploadFile ufile : fileList) {
                filesMap.put(ufile.getParameterName(), ufile);
            }

        }

        return filesMap;
    }


    /**
     * 默认的Index方法
     */
    public abstract void index();

    @NotAction
    public boolean isPostRequest() {
        return "POST".equalsIgnoreCase(this.getRequest().getMethod());
    }

    @NotAction
    public void renderAjaxFailed() {
        this.renderAjaxFailed(BaseConstants.RETURN_CODE_VALUE_FAILED, BaseConstants.RETURN_MESSAGE_VALUE_FAILED, null);
    }

    @NotAction
    public void renderAjaxFailed(int code, String msg) {
        this.renderAjaxFailed(code, msg, null);
    }

    @NotAction
    public void renderAjaxFailed(int code, String msg, Kv kv) {
        this.renderJson(
            Kv.by(BaseConstants.RETURN_PARA_NAME_SUCCESS, false).set(BaseConstants.RETURN_PARA_NAME_CODE, code)
            .set(BaseConstants.RETURN_PARA_NAME_MESSAGE, msg).set("data", kv));
    }

    @NotAction
    public void renderAjaxFailed(String msg) {
        this.renderAjaxFailed(BaseConstants.RETURN_CODE_VALUE_FAILED, msg, null);
    }

    @NotAction
    public void renderAjaxSuccessed() {
        this.renderAjaxSuccessed(null);
    }

    @NotAction
    public void renderAjaxSuccessed(Object obj) {
        this.renderJson(Kv.by(BaseConstants.RETURN_PARA_NAME_SUCCESS, true)
            .set(BaseConstants.RETURN_PARA_NAME_CODE, BaseConstants.RETURN_CODE_VALUE_SUCCESSED)
            .set(BaseConstants.RETURN_PARA_NAME_MESSAGE, BaseConstants.RETURN_MESSAGE_VALUE_SUCCESSED)
            .set(BaseConstants.RETURN_PARA_NAME_DATA, obj));
    }

}
